package com.doucat.piano.main;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.doucat.piano.KeyAdapter;
import com.doucat.piano.R;
import com.doucat.piano.SoundPlayUtils;
import com.doucat.piano.model.KeyBean;
import com.doucat.piano.model.KeyModel;
import com.doucat.support.component.fragment.base.BaseFragment;
import com.doucat.support.component.recycle.CommonRecyclerView;
import com.doucat.support.component.recycle.adapter.ItemClickListener;
import com.doucat.support.model.data.DataView;

import java.util.List;

/**
 * -----------------------------------------------------------------
 * Copyright (C) 2021-2031, by tonly, All rights reserved.
 * -----------------------------------------------------------------
 * File: PianoFragment.java
 * Author: wangfy
 * Create: 2021/7/27 16:49
 */
public class PianoFragment extends BaseFragment {
    private CommonRecyclerView listViewKey;
    private KeyAdapter adapter;
    private KeyModel keyModel;
    @Override
    public void onRootViewCreate(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        setBaseFragmentContentView(R.layout.fragment_paino);
        listViewKey=findViewById(R.id.listViewKey);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        listViewKey.setLayoutManager(layoutManager);
        adapter=new KeyAdapter(getContext());
        listViewKey.setAdapter(adapter);
        keyModel=new KeyModel();

        adapter.setOnItemClickListener(new ItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                KeyBean keyBean=adapter.getItem(position);
                SoundPlayUtils.getInstance(getContext()).play(keyBean.pid);
            }
        });

        keyModel.getKeys(new DataView<KeyBean>(this) {
            @Override
            public void onData(List data) {
                super.onData(data);
                adapter.setItems(data);
            }
        });
    }
}